/**
 * Copyright (c) 2019 EOVA.CN. All rights reserved.
 * 
 * Licensed under the EPPL license: http://eova.cn/eppl.txt
 * For authorization, please contact: admin@eova.cn
 */
package com.oss.shop;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.utils.xx;

public class ShopIntercept extends MetaObjectIntercept {

	@Override
	public void queryBefore(AopContext ac) throws Exception {

		String corporation = ac.user.getStr("corporation");
		String department = ac.user.getStr("department");
		String company = ac.user.getStr("company");
		int hotel_id=ac.user.get("hotel_id");

		if(!xx.isEmpty(corporation)){
			ac.condition = " and corporation = '" + corporation+"' ";
		}
		if(!xx.isEmpty(company)){
			ac.condition = " and company = '" + company+"' ";
		}
		if(!xx.isEmpty(department)){
			ac.condition = " and department = '" + department+"' ";
		}
		if(0!=hotel_id){
			ac.condition = " and id = " + hotel_id;
		}
	}

	public String addBefore(AopContext ac) throws Exception {

//		ac.record.set("create_time", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
//		String pro= Area.dao.getNameById(ac.record.get("province")+"");
//		String city= Area.dao.getNameById(ac.record.get("city")+"");
//		String region= Area.dao.getNameById(ac.record.get("region")+"");
//		String address = ac.record.get("address")+"";
//		String point=sendGetByHttpUrlForMap(pro+city+region+address);
//		JSONObject json=JSON.parseObject(point);
//		String status=json.get("status").toString();
//		if("1".equals(status)){
//			return Easy.warn("确认地址输入正确！ ");
//		}
//		if("0".equals(status)){
//			JSONObject json2=JSON.parseObject(((JSONObject) json.get("result")).getString("location"));
//			ac.record.set("point_x",json2.getString("lat").substring(0, 10));
//			ac.record.set("point_y",json2.getString("lng").substring(0, 10));
//		}
		return super.addBefore(ac);
	}

	/**
	 * 更新前置 自动设置更新时间(将update_time字段设为禁止修改，由此提供数据)
	 */
	@Override
	public String updateBefore(AopContext ac) throws Exception {
//		String pro= Area.dao.getNameById(ac.record.get("province")+"");
//		String city= Area.dao.getNameById(ac.record.get("city")+"");
//		String region= Area.dao.getNameById(ac.record.get("region")+"");
//		String address = ac.record.get("address")+"";
//		String point=sendGetByHttpUrlForMap(pro+city+region+address);
//		JSONObject json=JSON.parseObject(point);
//		String status=json.get("status").toString();
//		if("1".equals(status)){
//			return Easy.warn("确认地址输入正确！ ");
//		}
//		if("0".equals(status)){
//			JSONObject json2=JSON.parseObject(((JSONObject) json.get("result")).getString("location"));
//			ac.record.set("point_x",json2.getString("lat").substring(0, 10));
//			ac.record.set("point_y",json2.getString("lng").substring(0, 10));
//		}
		return super.updateBefore(ac);
	}

	public String sendGetByHttpUrlForMap(String address) {
//		StringBuffer resultBuffer = new StringBuffer();
//		// 构建请求参数
//		String urlStr = "http://api.map.baidu.com/geocoder/v2/?address=" + address.trim()
//				+ "&output=json&ak=m07L7mGBXlGCG3NVxlOYKCdsxl7o3swL";
//		HttpURLConnection con = null;
//		BufferedReader br = null;
//		InputStreamReader read = null;
//		try {
//			URL url = new URL(urlStr);
//			con = (HttpURLConnection) url.openConnection();
//			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//			con.connect();
//			resultBuffer = new StringBuffer();
//			read = new InputStreamReader(con.getInputStream(), "utf-8");
//			br = new BufferedReader(read);
//			String temp;
//			while ((temp = br.readLine()) != null) {
//				resultBuffer.append(temp);
//			}
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		} finally {
//			Constants.safeClose(read);
//			Constants.safeClose(br);
//		}
		return "";
	}

}